/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.songs.dao.hibernate;

import br.com.fatecpg.songs.dao.util.HibernateSession;
import br.com.fatecpg.songs.entity.User;
import br.com.fatecpg.songs.entity.util.UserStatus;
import java.io.Serializable;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Marcos
 */
public class UserDAO extends GenericDAO<User> implements br.com.fatecpg.songs.dao.UserDAO{
    public UserDAO(){
        super(User.class);
    }
    
    @Override
    public User login(User user){
        Session session = HibernateSession.currentSession();
        Criteria criteria = session.createCriteria(User.class)
                .add(Restrictions.eq("loginName", user.getLoginName()))
                .add(Restrictions.eq("password", user.getPassword()))
                .add(Restrictions.eq("userStatus", UserStatus.Y));
        return (User) criteria.uniqueResult();
    }

    @Override
    public void deactive(Serializable id) {
        User user = load(id);
        user.setUserStatus(UserStatus.N);
        update(user);
    }

    @Override
    public void active(Serializable id) {
        User user = load(id);
        user.setUserStatus(UserStatus.Y);
        update(user);
    }
}
